import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
const daoName = "HKZXQYDKDR";
const ButtonGroup = Button.Group;
const columns = [
  { title: '导入结果', width: 200, sorter: true, key: 'DRJG', dataIndex: "DRJG", dispaly: true, canEdit: true, filter: true },
  { title: '导入年月', width: 120, sorter: true, key: 'KHYF', dataIndex: "KHYF", dispaly: true, canEdit: true, filter: true },
  { title: '客户名称', width: 250, sorter: true, key: 'KHMC', dataIndex: 'KHMC', dispaly: true, canEdit: true, export:true },
  { title: '企业规模', width: 160, sorter: true, key: 'QYGM', dataIndex: "QYGM", dispaly: true, canEdit: true, export:true },
  { title: '利率浮动', width: 100, sorter: true, key: 'LLFD', dataIndex: "LLFD", dispaly: true, canEdit: true, export:true },
  { title: '放款日期', width: 120, sorter: true, key: 'FKRQ', caption: "FKRQ", render: data => datetime.format(data.FKRQ, "yyyy-MM-dd"), dispaly: true, canEdit: true, export:true },
  { title: '到期日', width: 120, sorter: true, key: 'DQR', caption: "DQR", render: data => datetime.format(data.DQR, "yyyy-MM-dd"), dispaly: true, canEdit: true, export:true },
  { title: '放款金额(万)', width: 120, sorter: true, key: 'FKJE', dataIndex: "FKJE", dispaly: true, canEdit: true, export:true },
  { title: '费用金额(万)', width: 120, sorter: true, key: 'FYJE', dataIndex: "FYJE", dispaly: true, canEdit: true, export:true },
  { title: '已发放', width: 120, sorter: true, key: 'YFF', dataIndex: "YFF", dispaly: true, canEdit: true, export:true },
  { title: '到期应发放', width: 120, sorter: true, key: 'DQYFF', dataIndex: "DQYFF", dispaly: true, canEdit: true, export:true },
];
let controls = [
  { name: 'DRJG', required: true, type: 'text',ignore: true, label: '导入结果', grid: 12 / 24 },
  { name: 'KHYF', required: true, type: 'text', label: '导入年月', grid: 12 / 24,placeholder:"按YYYYMM格式输入,例如:201801",min: 6, max: 6},
  { name: 'KHMC', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'QYGM', required: true, type: 'text', label: '企业规模', grid: 12 / 24 },
  { name: 'LLFD', required: true, type: 'text', label: '利率浮动', grid: 12 / 24 },
  { name: "FKRQ", label: "放款日期", type: "date", grid: 12 / 24, dataType: "date" },
  { name: "DQR", label: "到期日", type: "date", grid: 12 / 24, dataType: "date" },
  { name: 'FKJE', required: true, type: 'text', label: '放款金额(万)', grid: 12 / 24 },
  { name: 'FYJE', required: true, type: 'text', label: '费用金额(万)', grid: 12 / 24 },
  { name: 'YFF', required: true, type: 'text', label: '已发放', grid: 12 / 24 },
  { name: 'DQYFF', required: true, type: 'text', label: '到期应发放', grid: 12 / 24 }
];
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class HkzxqydkdrForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    let sort = {
      field: 'KHYF',
      order: 'descend'
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20,sorter: sort }
    });
    this.loadOptionData();
  }
  componentWillReceiveProps(nextProps) {
    const { showImport } = this.props
    if (!nextProps.showImport && showImport) {
      this.props.dispatch({
        type: `${daoName}/fetch`,
        payload: { daoName, page: 1, pageSize: 20 }
      });
    }
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showImportHandler() {
    this.props.dispatch({
      type: `${daoName}_DataImport/setDisplay`,
      payload: { display: true },
    });
  };
  DownTmportHandler() {
    let columnHeaders = []
    columns.map((c, j) => {
        if (!c.export) {
            return
        }
        let header = {
            Name: c.key,
            Caption: c.title,
            Width: c.width
        }
        columnHeaders.push(header)
    })
    let form = document.createElement('form');//定义一个form表单
    let whereItems = [];
    whereItems.push({
      FieldName: "1",
      Operator: "<>",
      FieldValues: "1"
    })
    const whereCauses = []
    whereCauses.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    const orderItems = []
    form.style = "display:none";
    form.target = "_blank";
    form.method = "post";
    form.action = "/api/ExportXlsx/"+daoName;
    let input1 = document.createElement("input");
    input1.type = "hidden";
    input1.name = "WhereCauses";
    input1.value = JSON.stringify(whereCauses);
    let input2 = document.createElement("input");
    input2.type = "hidden";
    input2.name = "OrderItems";
    input2.value = JSON.stringify(orderItems)
    let input3 = document.createElement("input");
    input3.type = "hidden";
    input3.name = "ColumnHeaders";
    input3.value = JSON.stringify(columnHeaders);
    let input4 = document.createElement("input");
    input4.type = "hidden";
    input4.name = "PermID";
    input4.value = "" + this.props.permID;
    let input5 = document.createElement("input");
    input5.type = "hidden";
    input5.name = "UserID";
    input5.value = "" + this.props.userID;
    document.body.appendChild(form);//将表单放置在web中
    form.appendChild(input1);
    form.appendChild(input2);
    form.appendChild(input3);
    form.appendChild(input4);
    form.appendChild(input5);
    form.submit();
    document.body.removeChild(form)
  }
  render() {
    let { showEdit, record, showImport, permID, popedomTypes } = this.props
    const pTypes = popedomTypes[permID]
    let data = null
    if (pTypes.indexOf("A") !== -1) {
      data = <ButtonGroup style={{ zIndex: 0 }}>
        <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
        <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
      </ButtonGroup>
    }
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {data}
          </div>
          <DataTable ref="WBTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
          <DataImport title="还款中小企业贷款" importName="HKZXQYDKDR" daoName="HKZXQYDKDR" keys={["ID"]} paramNames={["KHYF"]} importMode="DeleteAfterInsert" controls={controls} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  const { display } = state[`${daoName}_DataImport`];
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    showImport: display
  }
}
HkzxqydkdrForm = connect(mapStateToProps)(HkzxqydkdrForm)
export default HkzxqydkdrForm;

let render = (props) => React.createElement(HkzxqydkdrForm, props);
let f = new Factory()
f.set("HkzxqydkdrForm", render)
